package com.tale.ssm.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.tale.ssm.entity.Student;
import com.tale.ssm.entity.User;
import com.tale.ssm.service.StudentService;
import com.tale.ssm.service.UserService;
import com.tale.ssm.util.VCodeUtil;

@Controller
@RequestMapping("/system")
public class LoignController {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private UserService userService;
	
//	@RequestMapping(value = "/login", method = RequestMethod.GET)
//	public String loginG() {
//		System.out.println("get_login=======");
//		
//		return "/system/login";
//	}
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView model) {
		model.setViewName("system/index");
		System.out.println("index===========");
		return model;
	}
	
	
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView loginG(ModelAndView model) {
		System.out.println("get_login=======");
		model.setViewName("/system/login");
		return model;
	}
	
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> login(
			@RequestParam(value="userName",required=true) String userName,
			@RequestParam(value="password",required=true) String password,
			@RequestParam(value="vcode",required=true) String vCode,
			@RequestParam(value="type",required=true) int type,
			HttpServletRequest request
			){
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "error");
		if (userName.isEmpty() || userName.equals("")) {
//			map.put("type", "error");
			map.put("msg", "用户名不能为空");
			return map;
		}
		
		if (password.isEmpty() || password.equals("")) {
//			map.put("type", "error");
			map.put("msg", "密码不能为空");
			return map;
		}
	
		if (vCode.isEmpty() || vCode.equals("")) {
//			map.put("type", "error");
			map.put("msg", "验证码不能为空");
			return map;
		}
		String vCodeGenerator = (String) request.getSession().getAttribute("vCode");
		if(StringUtils.isEmpty(vCodeGenerator)) {
			map.put("msg", "长时间未操作，会话已失效，请刷新后重试!");
			return map;
		}
		
		if(!vCodeGenerator.toUpperCase().equals(vCode.toUpperCase())) {
			map.put("msg", "验证码错误");
			return map;
		}
		
		if(type == 1) {
			User user = userService.findByUserName(userName);
//			map.put("type", "error");
			if(user == null) {
				map.put("type", "error");
				map.put("msg", "不存在该用户!");
				return map;
			}
			if(!password.equals(user.getPassword())) {
//				map.put("type", "error");
				map.put("msg", "密码错误!");
				return map;
			}
			
			request.getSession().setAttribute("user", user);
		}else if(type == 2) {
			Student student = studentService.findByUserName(userName);
			if(student == null) {
				map.put("type", "error");
				map.put("msg", "不存在该用户!");
				return map;
			}
			if(!password.equals(student.getPassword())) {
//				map.put("type", "error");
				map.put("msg", "密码错误!");
				return map;
			}
			request.getSession().setAttribute("user", student);
		}
		
		map.put("msg", "登陆成功");
		map.put("type", "success");
		request.getSession().setAttribute("userType", type);
		return map;
	}
	/*
	 * 验证码
	 */
	@RequestMapping("/get_vCode")
	public void get_vCode(HttpServletRequest request,
			@RequestParam(value="len", defaultValue = "4", required = false) Integer len,
			@RequestParam(value="w",defaultValue="98",required=false) Integer w,
			@RequestParam(value="h",defaultValue="33",required=false) Integer h,
			HttpServletResponse response
			) {
		System.out.println("验证码===========");
		VCodeUtil util = new VCodeUtil();
		String vCode = util.generatorVCode();
		request.getSession().setAttribute("vCode", vCode);
		BufferedImage vCodeImage = util.generatorRotateVCodeImage(vCode, true);
		try {
			ImageIO.write(vCodeImage, "gif", response.getOutputStream());
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	
}
